package com.orange.lockscreensettings.version;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.orange.lockscreensettings.R;

public class ShowVersionsActivity extends Activity implements
		OnDismissListener, ListAdapter {

	private static final String		TAG							= "ShowVersionsActivity";

	private static final String		PACKAGE_LOCKSCREEN			= "com.orange.lockscreen";
	private static final String		PACKAGE_LOCKSCREEN_SERVICE	= "com.orange.lswidget";
	private static final String		PACKAGE_LOCKSCREEN_SETTINGS	= "com.orange.lockscreensettings";
	private static final String[]	PACKAGE_ITEMS				= {
			PACKAGE_LOCKSCREEN, PACKAGE_LOCKSCREEN_SERVICE,
			PACKAGE_LOCKSCREEN_SETTINGS						};

	private static final String		TITLE_LOCKSCREEN			= " Lockscreen";
	private static final String		TITLE_LOCKSCREEN_SERVICE	= " Lockscreen service";
	private static final String		TITLE_LOCKSCREEN_SETTINGS	= " Lockscreen settings";
	private static final String[]	TITLE_ITEMS					= {
			TITLE_LOCKSCREEN, TITLE_LOCKSCREEN_SERVICE,
			TITLE_LOCKSCREEN_SETTINGS							};

	private static final String		COLON						= " : ";
	private static final String		NOT_INSTALLED				= "Not installed";

	private static final int		DIALOG_LIST					= 0;

	private static final StyleSpan	STYLE_BOLD					= new StyleSpan(
																		Typeface.BOLD);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		showDialog(DIALOG_LIST);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case DIALOG_LIST:
			Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(getString(R.string.build_no));
			builder.setAdapter(this, null);
			builder.setPositiveButton("OK", null);
			dialog = builder.create();

			dialog.setOnDismissListener(this);
			break;
		default:
			break;
		}
		return dialog;
	}

	@Override
	public void onDismiss(DialogInterface dialog) {
		finish();
	}

	private String getVersionName(String packageName) {
		String versionName = "";

		try {
			PackageInfo info = getPackageManager().getPackageInfo(packageName,
					PackageManager.GET_ACTIVITIES);
			versionName = info.versionName;
		} catch (NameNotFoundException e) {
		}

		return versionName;
	}

	private CharSequence getVersionName(int index) {
		final int color = Color.RED;
		SpannableStringBuilder sb = new SpannableStringBuilder();
		Object object = null;
		sb.append(TITLE_ITEMS[index]);
		sb.append(COLON);
		int before = sb.length();
		String versionName = getVersionName(PACKAGE_ITEMS[index]);
		if (TextUtils.isEmpty(versionName)) {
			versionName = NOT_INSTALLED;
			object = new ForegroundColorSpan(color);
		} else {
			object = (Object) STYLE_BOLD;
		}
		sb.append(versionName);
		sb.setSpan(object, before, sb.length(),
				Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		return sb;
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {
	}

	@Override
	public boolean isEmpty() {
		return TITLE_ITEMS.length > 0;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public int getViewTypeCount() {
		return TITLE_ITEMS.length;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TextView tv = new TextView(getApplicationContext());
		tv.setText(getVersionName(position));
		tv.setTextColor(getResources().getColor(
				android.R.color.primary_text_light));
		if (position == 0) {
			tv.setPadding(0, 8, 0, 8);
		} else if (position == TITLE_ITEMS.length) {
			tv.setPadding(0, 0, 0, 16);
		} else {
			tv.setPadding(0, 0, 0, 8);
		}
		return tv;
	}

	@Override
	public int getItemViewType(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public int getCount() {
		return TITLE_ITEMS.length;
	}

	@Override
	public boolean isEnabled(int position) {
		return false;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

}
